import Error from "@/views/error.vue";
import Login from "@/views/admin/login.vue";
import Home from "@/views/admin/home.vue";
import Front from "@/views/front.vue";
import { getToken } from "@/utils/cache";
import { createWebHistory, createRouter, RouteRecordRaw } from "vue-router";
import NProgress from "nprogress";
import "nprogress/nprogress.css";

/**
 * 框架基础路由
 */
const routes: Array<RouteRecordRaw> = [
  {
    path: "/admin",
    component: Home,
    redirect: "/login",
    meta: { title: "主页" },
    children: [
      {
        path: "user/list",
        name: "sysUser", // 用户信息
        component: () => import("@/views/admin/system/user/index.vue")
      },{
        path: "travel/food/list",
        name: "adminTravelList", // 京剧信息管理美食
        component: () => import("@/views/admin/display/foodList.vue")
      },{
        path: "display/list",
        name: "adminDisplayList", // 京剧展示信息管理
        component: () => import("@/views/admin/content/displayList.vue")
      },{
        path: "activity/list",
        name: "adminNewsList", // 活动信息管理
        component: () => import("@/views/admin/activity/activityList.vue")
      },{
        path: "sys/config/list",
        name: "configManage", // 系统参数管理
        component: () => import("@/views/admin/system/config/index.vue")
      },{
        path: "sys/profile",
        name: "adminProfile", // 管理员个人信息
        component: () => import("@/views/admin/system/user/adminProfile.vue")
      },{
        path: "goods/list",
        name: "goodsList", // 商品信息管理
        component: () => import("@/views/admin/goods/index.vue")
      },{
        path: "order/list",
        name: "adminOrderList", // 订单列表
        component: () => import("@/views/admin/goods/orderList.vue")
      },{
        path: "blog/list",
        name: "adminBlogList", // 论坛管理
        component: () => import("@/views/admin/forum/blogList.vue"),
      },{
        path: "comment/list",
        name: "adminCommentList", // 评论管理
        component: () => import("@/views/admin/forum/commentList.vue"),
      },{
        path: "sys/config/slide",
        name: "slidPicManage", // 评论管理
        component: () => import("@/views/admin/system/slidePic/index.vue"),
      },{
        path: "course/list",
        name: "adminCourse", // 学习资源管理
        component: () => import("@/views/admin/course/adminCourseList.vue"),
      },{
        path: "question/list",
        name: "adminQuestion", // 试题管理
        component: () => import("@/views/admin/course/questionList.vue"),
      },
    ]
  },{
    //前台页面路由
    path: "/",
    component: Front,
    redirect: "/opera/index",
    meta: { title: "主页" },
    children: [
      {
        path: "opera/index",
        name: "frontIndex2", // 用户信息
        component: () => import("@/views/front/IndexPage.vue"),
      },
      // {
      //   path: "user/register",
      //   name: "userRegist", // 用户注册
      //   component: () => import("@/views/front/user/UserRegist.vue"),
      // },{
      //   path: "user/login",
      //   name: "userLogin", // 用户登录
      //   component: () => import("@/views/front/user/UserLogin.vue"),
      // },
      {
        path: "user/profile",
        name: "userProfile", // 用户中心
        component: () => import("@/views/front/user/UserProfile.vue"),
      },{
        path: "activity/detail",
        name: "activityDetail", // 活动详情页面
        component: () => import("@/views/front/content/activityDetail.vue"),
      },{
        path: "activity/list",
        name: "activityList", // 活动列表
        component: () => import("@/views/front/content/activityList.vue"),
      },{
        path: "activity/detail",
        name: "activityDetail", // 活动详情
        component: () => import("@/views/front/content/activityDetail.vue"),
      },{
        path: "display/list",
        name: "frontDisplayList", // 京剧展示
        component: () => import("@/views/front/content/operaDisplayList.vue"),
      },{
        path: "display/detail",
        name: "frontDisplayDetail", // 京剧详情
        component: () => import("@/views/front/content/operaDisplayDetail.vue"),
      },{
        path: "course/list",
        name: "frontCourseList", // 教学资源列表
        component: () => import("@/views/front/content/courseList.vue"),
      },{
        path: "course/detail",
        name: "frontCourseDetail", // 教学资源详情
        component: () => import("@/views/front/content/courseDetail.vue"),
      },{
        path: "question/index",
        name: "frontQuestion", // 知识趣答
        component: () => import("@/views/front/content/questionIndex.vue"),
      },{
        path: "blog/list",
        name: "blogList", // 论坛
        component: () => import("@/views/front/content/blogList.vue"),
      },{
        path: "blog/detail",
        name: "blogDetail", // 笔记详情
        component: () => import("@/views/front/content/blogDetail.vue"),
      },{
        path: "activity/signin",
        name: "sportRecord", //报名的活动
        component: () => import("@/views/front/record/signinActivityRecord.vue"),
      },{
        path: "activity/publish",
        name: "dietRecord", // 发起的京剧活动记录
        component: () => import("@/views/front/record/publishActivityRecord.vue"),
      },{
        path: "user/fav",
        name: "favList", // 收藏夹
        component: () => import("@/views/front/content/myFav.vue"),
      },{
        path: "product/list",
        name: "frontProductList", // 积分商城
        component: () => import("@/views/front/product/ProductList.vue"),
      },{
        path: "order/confirm",
        name: "orderConfirm", // 确认订单
        component: () => import("@/views/front/product/OrderConfirm.vue"),
      },{
        path: "order/my/list",
        name: "myOrderList", // 我的订单
        component: () => import("@/views/front/product/MyOrders.vue"),
      },{
        path: "user/my/blog",
        name: "myBlogList", // 我的帖子
        component: () => import("@/views/front/content/myBlog.vue"),
      },
    ]
  },{
    path: "/admin/login",
    component: Login,
    meta: { title: "登录" }
  },
  {
    path: "/opera",
    component: () => import("@/views/front/IndexPage.vue"),
    meta: { title: "前台首页" }
  },
  {
    path: "/user/login",
    component: () => import("@/views/front/user/UserLogin.vue"),
    meta: { title: "前台登录" }
  },{
    path: "/user/register",
    component: () => import("@/views/front/user/UserRegist.vue"),
    meta: { title: "前台注册" }
  },
  {
    path: "/error",
    name: "error",
    component: Error,
    meta: { title: "错误页面" }
  },
  {
    path: "/:path(.*)*",
    redirect: { path: "/error", query: { to: 404 }, replace: true }
  }
];

const router = createRouter({
  history: createWebHistory(),
  routes: routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      return { top: 0 };
    }
  }
});

// 设置路由守卫
// router.beforeEach((to, from, next) => {
//   // 执行进度条开始加载
//   NProgress.start();
//   const token = getToken();
//   if (to.path === "/login") {
//     if (token) next({ path: "/" });
//   } else {
//     if (!token) next({ path: "/login" });
//   }
//   next();
// });

// // 路由跳转后钩子函数中 - 执行进度条加载结束
// router.afterEach(() => {
//   NProgress.done();
// });

export default router;
